﻿using System;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Xml.Linq;

namespace Pixster.Slideshow.BusinessObjects
{
    public class ServiceFacade : INotifyPropertyChanged
    {
        private string _hostName;
        private PhotoSlideShow _currentSlideShow;

        public void Load() {

            _hostName = Application.Current.Host.Source.AbsoluteUri.Replace(Application.Current.Host.Source.LocalPath, "");

            this.LoadXmlFile();
        }

        public PhotoSlideShow CurrentSlideShow
        {
            get
            {
                return this._currentSlideShow;
            }
            set
            {
                if (ReferenceEquals(this.CurrentSlideShow, value))
                    return;

                this._currentSlideShow = value;
                this.OnPropertyChanged("CurrentSlideShow");
            }
        }

        public event EventHandler Loaded;

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string info)
        {
            var temp = this.PropertyChanged;
            if (temp != null)
                temp(this, new PropertyChangedEventArgs(info));
        }

        #endregion

        private void LoadXmlFile()
        {
            var xmlClient = new WebClient();
            xmlClient.DownloadStringCompleted += OnXmlFileLoaded;
            xmlClient.DownloadStringAsync(new Uri("/Photos/sitemap.xml", UriKind.RelativeOrAbsolute));
        }

        void OnXmlFileLoaded(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null) return;
            try
            {
                var xdoc = XDocument.Parse(e.Result);

                var photoList = (xdoc.Descendants("PixDirectory")
                    .Elements("Albums")
                    .Elements("PixAlbum")
                    .Elements("Photos")
                    .Elements("PixPhoto")
                    .Select(feed => new Photo
                                        (
                                        new Uri(string.Format("{0}/Photos/{1}/{2}"
                                                              , _hostName
                                                              , feed.Element("VirtualPath").Value.Replace("\\", "/")
                                                              , feed.Element("FileName").Value)
                                                , UriKind.RelativeOrAbsolute)
                                        ))).ToList();

                var collection = new ObservableCollection<Photo>();

                photoList.ForEach(collection.Add);

                this.CurrentSlideShow = new PhotoSlideShow(collection);
            }
            catch (Exception)
            {
                //DisplayErrorImage();
            }
            if (Loaded != null)
            {
                Loaded(this, EventArgs.Empty);
            }
        }
    }
}
